package com.bjh.controller;

import com.bjh.service.SaleService;
import com.bjh.util.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/agent/sale")
public class SaleController extends BaseController {
    @Autowired
    private SaleService saleService;

    /**
     * 获取当前代理商所属销售人员
     * @param agentId 代理商id
     * @param status 销售人员状态：0：在职，1：离职
     * @return {@link Map}
     */
    @GetMapping("/getSaleByAgentId")
    public Map<String, Object> getSaleByAgentId (@RequestParam Long agentId, @RequestParam Long status){
        logBefore(log, "++++++++++ 获取在职销售人员 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            params.put("agentId", agentId);
            params.put("status", String.valueOf(status));
            List<?> resList= new ArrayList<>(saleService.getSaleByAgentId(params));
            logAfter(log);
            return returnSuccessWithData(resList);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "编辑失败");
        }
    }
    /**
     * 分页获取当前代理商所属销售人员
     * @param agentId 代理商id
     * @param status 销售人员状态：0：在职，1：离职
     * @param search 搜索关键字
     * @param pageIndex 起始页
     * @param pageSize 分页大小
     * @return {@link Map}
     */
    @GetMapping("/getSaleOfPageByAgentId")
    public Map<String, Object> getSaleOfPageByAgentId (@RequestParam Long agentId, @RequestParam(required = false) Long status,
        @RequestParam(required = false) String search, @RequestParam Long pageIndex, @RequestParam Long pageSize){
        logBefore(log, "++++++++++ 分页获取当前代理商所属在职销售人员 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            params.put("agentId", agentId);
            if(Objects.nonNull(status)){
                params.put("status", String.valueOf(status));
            }else{
                params.put("status", "0");
            }
            if(Objects.nonNull(search)) params.put("search", search);
            params.put("start", (pageIndex - 1) * pageSize);
            params.put("length", pageSize);
            Map<String, Object> resMap= new HashMap<>(saleService.getSaleOfPageByAgentId(params));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "编辑失败");
        }
    }

    /**
     * 获取销售人员详情
     * @param id 销售人员id
     * @return {@link Map}
     */
    @GetMapping("/getSaleById")
    public Map<String, Object> getSaleById (@RequestParam Long id){
        logBefore(log, "++++++++++ 获取销售人员详情 ++++++++++");
        try{
            Map<String, Object> resMap = new HashMap<>(saleService.getSaleById(id));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }
    /**
     * 编辑销售人员
     * @param id 销售人员id
     * @param agentId 代理商id
     * @param name 销售人员名称
     * @param mobile 手机号
     * @param legalCardNumber 销售人员身份证号码
     * @return {@link Map}
     */
    @PostMapping("/editSale")
    public Map<String, Object> editSale (@RequestParam(required = false) Long id, @RequestParam Long agentId,
    @RequestParam String name, @RequestParam String mobile, @RequestParam String legalCardNumber){
        logBefore(log, "++++++++++ 编辑销售人员 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            if(Objects.nonNull(id)) params.put("id", id);
            params.put("agentId", agentId);
            params.put("name", name);
            params.put("mobile", mobile);
            params.put("legalCardNumber", legalCardNumber);
            params.put("status", "0");
            saleService.editSale(params);
            logAfter(log);
            return returnSuccess();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "编辑失败");
        }
    }
}
